package com.ruoyi.web.controller.blog;

import com.ruoyi.blog.domain.bo.BlogArticleBo;
import com.ruoyi.blog.domain.export.BlogArticleContentExport;
import com.ruoyi.blog.domain.vo.BlogArticleVo;
import com.ruoyi.blog.service.IBlogArticleService;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.annotation.RepeatSubmit;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.validate.AddGroup;
import com.ruoyi.common.core.validate.EditGroup;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.util.Arrays;
import java.util.List;

/**
 * 博客文章Controller
 *
 * @author zhutianyu
 * @date 2021-08-19
 */
@Validated
@Api(value = "博客文章控制器", tags = {"博客文章管理"})
@RequiredArgsConstructor(onConstructor_ = @Autowired)
@RestController
@RequestMapping("/blog/article")
public class BlogArticleController extends BaseController {

    private final IBlogArticleService iBlogArticleService;

    /**
     * 查询博客文章列表
     */
    @ApiOperation("查询博客文章列表")
    @PreAuthorize("@ss.hasPermi('blog:article:list')")
    @GetMapping("/list")
    public TableDataInfo<BlogArticleVo> list(@Validated BlogArticleBo bo) {
        return iBlogArticleService.queryPageList(bo);
    }

    /**
     * 导出博客文章列表
     */
    @ApiOperation("导出博客文章列表")
    @PreAuthorize("@ss.hasPermi('blog:article:export')")
    @Log(title = "博客文章", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult<BlogArticleVo> export(@Validated BlogArticleBo bo) {
		List<BlogArticleContentExport> exports = iBlogArticleService.articleExport(bo);
		ExcelUtil<BlogArticleContentExport> util = new ExcelUtil<>(BlogArticleContentExport.class);
        return util.exportExcel(exports, "博客文章");
    }

    /**
     * 获取博客文章详细信息
     */
    @ApiOperation("获取博客文章详细信息")
    @PreAuthorize("@ss.hasPermi('blog:article:query')")
    @GetMapping("/{id}")
    public AjaxResult<BlogArticleVo> getInfo(@NotNull(message = "主键不能为空")
                                             @PathVariable("id") Long id) {
        return AjaxResult.success(iBlogArticleService.queryById(id));
    }

    /**
     * 新增博客文章
     */
    @ApiOperation("新增博客文章")
    @PreAuthorize("@ss.hasPermi('blog:article:add')")
    @Log(title = "博客文章", businessType = BusinessType.INSERT)
    @RepeatSubmit
    @PostMapping()
    public AjaxResult<Void> add(@Validated(AddGroup.class) @RequestBody BlogArticleBo bo) {
        return toAjax(iBlogArticleService.insertByBo(bo));
    }

    /**
     * 修改博客文章
     */
    @ApiOperation("修改博客文章")
    @PreAuthorize("@ss.hasPermi('blog:article:edit')")
    @Log(title = "博客文章", businessType = BusinessType.UPDATE)
    @RepeatSubmit
    @PutMapping()
    public AjaxResult<Void> edit(@Validated(EditGroup.class) @RequestBody BlogArticleBo bo) {
        return toAjax(iBlogArticleService.updateByBo(bo));
    }

    /**
     * 删除博客文章
     */
    @ApiOperation("删除博客文章")
    @PreAuthorize("@ss.hasPermi('blog:article:remove')")
    @Log(title = "博客文章", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult<Void> remove(@NotEmpty(message = "主键不能为空")
                                   @PathVariable Long[] ids) {
        return toAjax(iBlogArticleService.deleteWithValidByIds(Arrays.asList(ids), true));
    }

    /**
     * 根据文章id修改展示状态
     */
    @ApiOperation("修改文章展示状态")
    @PreAuthorize("@ss.hasPermi('blog:article:updateStatus')")
    @Log(title = "博客文章展示状态", businessType = BusinessType.UPDATE)
    @PatchMapping("status/{id}")
    public AjaxResult<Void> updateStatus(@NotNull(message = "主键不能为空") @PathVariable("id") Long id) {
        return toAjax(iBlogArticleService.updateStatus(id));
    }

}
